package com.sbr.esbmonitor.repositories.xml.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.sbr.esbmonitor.model.xml.servers.ServerType;
import com.sbr.esbmonitor.model.xml.servers.ServersType;
import com.sbr.esbmonitor.repositories.ServerRepository;
import com.sbr.esbmonitor.repositories.xml.JAXBUtils;
import com.sbr.esbmonitor.servicemix.Server;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XMLServerRepository implements ServerRepository {


	private static final Logger LOG = LoggerFactory.getLogger(XMLServerRepository.class);

	private JAXBUtils jaxbUtils = new JAXBUtils();
	
	@Override
	public List<Server> selectAll() {
		LOG.debug(">>selectAll()");
//		JAXBContext context;
//		ServersType serversType = new ServersType();
//		List<Server> servers = new ArrayList<Server>();
//		try {
//			context = JAXBContext.newInstance("com.sbr.esbmonitor.model.xml.servers");
//			Unmarshaller unMarshaller = context.createUnmarshaller();
//			InputStream is = new FileInputStream(new File("servers.xml"));
//			serversType = (ServersType) unMarshaller.unmarshal(is);
//		} catch (JAXBException e) {
//			LOG.error("Error lors du Unmarshalling",e);
//		} catch (FileNotFoundException e) {
//			LOG.error("Le fichier servers.xml n'a pas &été trouvé",e);
//		}
		
		ServersType serversType = jaxbUtils.unmarshal("servers.xml");
		List<Server> servers = new ArrayList<Server>();
		for (ServerType serverType : serversType.getServer()) {
			if (serverType != null) { //TODO c'est nécessaire ?
				servers.add(new Server(serverType));
			}
		}
		return servers;
	}

	@Override
	public Server selectById(long id) {
//		JAXBContext context;
//		ServersType serversType = new ServersType();
//		Server server = null;
//		try {
//			context = JAXBContext.newInstance("com.sbr.esbmonitor.model.xml.servers");
//			Unmarshaller unMarshaller = context.createUnmarshaller();
//			InputStream is = new FileInputStream(new File("servers.xml"));
//			serversType = (ServersType) unMarshaller.unmarshal(is);
//		} catch (JAXBException e) {
//			LOG.error("Error lors du Unmarshalling",e);
//		} catch (FileNotFoundException e) {
//			LOG.error("Le fichier servers.xml n'a pas &été trouvé",e);
//		}
		ServersType serversType = jaxbUtils.unmarshal("servers.xml");
		Server server = null;
		for (ServerType serverType : serversType.getServer()) {
			if (serverType != null && serverType.getId() == id) { 
				server = new Server(serverType);
			}
		}
		return server;
	}

	@Override
	public void insert(Server server) {

		ServerType xmlServer = server.toXML();

		
		ServersType serversType = new ServersType();
		serversType.getServer().add(xmlServer);

		// on récupère les autres
		List<Server> servers = selectAll();

		if (servers != null) {
			for (Server previousServer : servers) {
				serversType.getServer().add(previousServer.toXML());
			}
		}

		jaxbUtils.marshal("servers.xml", serversType);
//		JAXBContext context;
//		try {
//			context = JAXBContext.newInstance("com.sbr.esbmonitor.model.xml.servers");
//			Marshaller marshaller = context.createMarshaller();
//
//			OutputStream os = new FileOutputStream(new File("servers.xml"));
//			marshaller.marshal(serversType, os);
//		} catch (JAXBException e) {
//			LOG.error("Error lors du Unmarshalling",e);
//		} catch (FileNotFoundException e) {
//			LOG.error("Le fichier servers.xml n'a pas &été trouvé",e);
//		}
	}

	@Override
	public boolean update(Server server) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteById(long id) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setJAXBUtils(JAXBUtils jaxbUtils) {
		this.jaxbUtils = jaxbUtils;
	}

}
